<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\service\statSign\Basic;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class CarouselBannerWithPos extends BaseModule
{

    const FORMAT_HIT = 'formatPosHit';
    public static $map = [
        1 => 1,
        2 => 2,
        3 => 3,
        4 => 4,
        5 => 5,
        6 => '兜底1',
        7 => '兜底2',
        8 => '兜底3',
    ];

    public function homeChannelV6($hits, $extendStatSignExt)
    {

        $bannerRows = [];
        $i = 1;
        foreach ($hits as $v) {
            $statSignExt = [\Doraemon\service\statSign\Basic::EXTEND_KEY_KEY_WORD => $v['title']] + $extendStatSignExt;
            if ($this->position_id == 118) {
                $statSignExt = [
                    Basic::EXTEND_KEY_CONFIG_ID => $v['id'],
                    Basic::EXTEND_KEY_POS => 'center',
                    Basic::EXTEND_KEY_INDEX => $i,
                ];
            }
            if ($this->position_id == 119) {
                $statSignExt = [
                    Basic::EXTEND_KEY_CONFIG_ID => $v['id'],
                    Basic::EXTEND_KEY_POS => 'bottom',
                    Basic::EXTEND_KEY_INDEX => $i,
                ];
            }
            $statSign = $this->statSign($v, $statSignExt);
            $bannerRows[] = [
                "type" => BlockType::TYPE_BANNER,
                "banner" => [
                    "title"    => $v['title'],
                    "pic_url"  => empty($v['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                    "small_icon"  => empty($v['single_pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['single_pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                    'play_times' => (int)$v['play_times'],
                    "link"     => $this->generateLink($v['link_type'], $v['link_params']),
                ],
                "statSign" => $statSign,
            ];
            $i++;
        }
        return [
            'type' => 2011, //BlockType::TYPE_CAROUSEL_BANNER_NEW
            'bannerRows' => $bannerRows,
        ];
    }

    public static function homeCarouselV5($hits)
    {
        foreach ($hits as $v) {
            if ($v['ext_type']) return [$v];
        }
        $hits = array_column($hits, null, 'pos');
        $topList = [];
        foreach ([3, 2, 1] as $k) {
            if (empty($hits[$k])) {
                if (!empty($hits[4])) {
                    $topList[] = $hits[4];
                    unset($hits[4]);
                    continue;
                }
                if (!empty($hits[5])) {
                    $topList[] = $hits[5];
                    unset($hits[5]);
                    continue;
                }
                $bak = $k + 5;
                if (!empty($hits[$bak])) {
                    $topList[] = $hits[$bak];
                    unset($hits[$bak]);
                    continue;
                }
            } else {
                $topList[] = $hits[$k];
            }
        }
        shuffle($topList);
        if (!empty($hits[4])) $topList[] = $hits[4];
        if (!empty($hits[5])) $topList[] = $hits[5];

        return $topList;
    }

    /**
     * 推荐线顶部轮播api格式化输出
     * @param $hits
     * @param $uid
     * @param $userProfileExt
     * @param $statSignExt
     * @return array
     */
    public function format($hits, $uid, $userProfileExt, $statSignExt)
    {
        $banner_list = [];
        $upgradeVersion = '5.21.0';
        $response = [
            'type' => BlockType::TYPE_CAROUSEL_BANNER,
            'banner_list' => &$banner_list,
        ];

        if ($hits) {
            $i = 1;
            $sort = array_column($hits, 'pos');
            array_multisort($sort, SORT_ASC, $hits);

            //三拼轮播规则
            //1,2,3, 三个位置随机顺序.
            //1,2,3 都需要有兜底物料.
            //4,5 都采取后补的方式进行填充.
            //前三位不足3广告时,优先使用4,5的物料,后使用兜底物料.
            if (in_array($this->position_id, [106, 118, 119])) return $this->homeChannelV6($hits, $statSignExt);
            if ($this->position_id == self::HOME_CAROUSEL_V5) $hits = self::homeCarouselV5($hits);

            foreach ($hits as $hit) {
                //公共信息赋值
                $id = intval($hit['id']);
                $index = intval($i);
                $hit['pos'] = $index;
                switch ($hit['position_id']) {
                    case self::TAB5_CREATIVE_ACTIVITY_CHANNEL:
                        $banner_list[] = [
                            'id' => $id,
                            'title' => $hit['title'] ?? '',
                            'index' => $index,
                            'banner' => empty($hit['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                            'link' => $this->generateLink($hit['link_type'], $hit['link_params']),
                            'statSign' => $this->statSign($hit, $statSignExt),
                        ];
                        break;
                    case 65:
                        $link = $hit['ext']['key_word'] ?? '';
                        $response['time'] = 3;
                        $banner_list[] = [
                            'id' => $id,
                            'index' => $index,
                            'banner' => empty($hit['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                            'link' => $link,
                        ];
                        break;
                    default:
                        $response['time'] = 3;
                        $linkParams = $hit['link_params'];
                        $statSign = $this->statSign($hit, $statSignExt);
                        $linkParams = self::addStatSignToLink($linkParams, $statSign);
                        $banner_list[] = [
                            'id' => $id,
                            'index' => $index,
                            'banner' => empty($hit['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                            'link' => $this->generateLink($hit['link_type'], $linkParams),
                            'statSign' => $statSign,
                        ];
                        break;
                }
                $i++;
            }
        }

        if ($this->position_id == self::HOME_CAROUSEL_V5) {
            if (version_compare(HhzClient::getClientVersion(), '6.0.0', '>=')) {
                foreach ($banner_list as &$v) {
                    $v['statSign'] = Basic::genStatSign('home-carousel_v5', [
                        Basic::EXTEND_KEY_CONFIG_ID => $v['id'],
                    ]);
                }
            }
            $banner_list = \Doraemon\tools\StatSign::versionCompatible($banner_list, $upgradeVersion, BlockType::TYPE_BANNER, 'banner');
            if (version_compare(HhzClient::getClientVersion(), $upgradeVersion, '>=')) {
                $response = [
                    'type' => BlockType::TYPE_CAROUSEL_BANNER_NEW,
                    'bannerRows' => &$banner_list,
                    'time' => 3,
                ];
            }
        }

        return $response;
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        if ($this->position_id == 65) {
            $v['ext'] = $map[$v['id']] ?? [];
        }
        if ($this->position_id != self::HOME_CAROUSEL_V5) {
            $keys = range(1, 100);
            self::$map = array_combine($keys, $keys);
        }
        $v['pos_name'] = self::$map[$v['pos']] ?? '-';
    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }
}